home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10497 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: grimsel.zurich.ibm.com!usenet
  2. From: wgk@zurich.ibm.com (Keith Whittingham)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: unsigned char question
  5. Date: 8 Mar 1996 08:34:47 GMT
  6. Organization: IBM Research, ZRH
  7. Message-ID: <4horf7$oif@grimsel.zurich.ibm.com>
  8. References: <3136864B.7154@eds.com>
  9. Reply-To: wgk@zurich.ibm.com
  10. NNTP-Posting-Host: pine.zurich.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.00
  12.  
  13. In <3136864B.7154@eds.com>, Perry Hoekstra <lnusmsc.phoeks01@eds.com> writes:
  14. >This is a basic question but one I cannot answer.  I am using VC++ 2.0
  15. >and I wish to assign a string to a variable of type UCHAR * (which is an unsigned char 
  16. >within ODBC).  The code is as follows
  17. >
  18. >UCHAR * server;
  19. >
  20. >server = "jmbademo";
  21. >
  22.  
  23. The other couple of responses to this say "cast" - yes your problem is
  24. that the types are different, server is unsigned char and "jmbademo" is
  25. a signed char. 
  26.  
  27. Using UCHAR, LONG, etc. is one of my pet hates: use "unsigned char" or 
  28. "long" instead. It's clearer, it takes less time to compile and
  29. what do think will happen if someone ever decides to change UCHAR to
  30. be something other than unsigned char? If you need some kind of data
  31. type portability use U16 for an unsigned 16 bit, S8 for a signed 8 bit.
  32. That way you know what your dealing with.
  33.  
  34. A second no-no is casting your way out of problems. Casting gets rid of
  35. compiler warnings and errors but is no solution. Worse still a cast 
  36. represents a maintenance problem. Consider:
  37.  
  38.     1: char *Ptr;
  39.     2: unsigned char  uc;
  40.     3: 
  41.     4: uc = 0;
  42.     5: Ptr = (char *)&uc;
  43.     6: (*Ptr)--;
  44.     7: cout << uc << endl;
  45.  
  46. uc has wrapped to 255 as one would expect. Now change line 2 to read
  47.  
  48.     2: unsigned int  uc;
  49.  
  50. No compiler errors, warnings and not the same answer.
  51.  
  52. If "server" is a signed char use a signed char otherwise what you're doing
  53. is problably wrong and it's going to bite back later.
  54.  
  55. KEith
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.